<template>
	<view class="login">
		<block>
			<view class="container">
				<!--插画-->
				<!-- <view class="right-top-sign"></view> -->
				<view class="logo-image tn-flex tn-flex-direction-row tn-flex-col-center">
					<image src="../../static/logo.png" mode="" class="tn-margin-right"></image>
					<text>融健科技居家上门服务系统</text>
				</view>
				<view class="backgroud-image tn-flex">
					<image src="../../static/login-bg.png" mode="widthFix"></image>
				</view>
				<!-- 设置白色背景防止软键盘把下部绝对定位元素顶上来盖住输入框等 -->
				<view class="wrapper">
					<!-- <view class="left-top-sign">LOGIN</view>
					<view class="welcome">
						欢迎回来！
					</view> -->
					<view class="" v-if="IsWxLogin">
						<button class="confirm-btn1" :disabled="btnLoading" :loading="btnLoading" @tap="wxLogin">
							手机号一键登录
						</button>
					</view>
					<view class="input-content" v-if="!IsWxLogin">

						<view class="input-item">
							<text class="tit">用户名</text>
							<input name="mobile" v-model="loginParams.mobile" placeholder="请输入用户名" maxlength="11"
								@blur="blurMobileChange" />
						</view>
						<view class="input-item" v-if="loginByPass">
							<text class="tit">密码</text>
							<input name="password" type="password" v-model="loginParams.password" placeholder="请输入密码"
								maxlength="20" />
						</view>

						<!-- <view class="input-item input-item-sms-code" v-if="!loginByPass">
							<view class="input-wrapper">
								<view class="oa-input-wrapper">
									<view class="tit">验证码</view>
									<input type="number" v-model="loginParams.code" placeholder="请输入验证码" maxlength="4"
										data-key="mobile" />
								</view>
								<button class="sms-code-btn" :disabled="smsCodeBtnDisabled"
									@tap.stop="getSmsCode('login')">
									<text v-if="!smsCodeBtnDisabled">获取验证码</text>
									<text v-else class="sms-code-resend">{{
										`重新发送 (${codeSeconds})`
										}}</text>
								</button>
							</view>
						</view> -->
						<button class="confirm-btn" :disabled="btnLoading" :loading="btnLoading" @tap="toLogin">
							登录
						</button>
					</view>
					<!-- <view @tap="showLoginBySmsCode" class="forget-section">
						{{ loginByPass ? '验证码登录' : '密码登录' }}
					</view> -->
				</view>
				<view class="register-section">
					<!-- 	<text @tap="navTo('/pages/public/password')">找回密码</text>
					或者 -->
					<text @tap="toHome">返回主页</text>
				</view>
			</view>
		</block>

	</view>
</template>
<script>
	/**
	 *@des 
	 *@author yiiframe 21931118@qq.com
	 *@blog http://www.yiiframe.com
	 *@date 2019/1/1 07:25:00
	 */
	export default {
		data() {
			return {
				loginParams: {
					mobile: '',
					code: '',
					password: ''
				},
				IsWxLogin: true,
				btnLoading: false,
				reqBody: {},
				loginByPass: true,
				smsCodeBtnDisabled: false,
				codeSeconds: 0 // 验证码发送时间间隔
			};
		},
		onShow() {},

		methods: {
			// ...mapMutations(['login']),

			// 失去焦点的手机号
			blurMobileChange(e) {
				this.mobile = e.detail.value;
			},
			// 切换登录方式
			showLoginBySmsCode() {
				this.loginByPass = !this.loginByPass;
			},
			// 返回上一页
			navBack() {
				this.$mRouter.back();
			},
			// 统一跳转路由
			navTo(route) {
				this.$mRouter.push({
					route
				});
			},
			// 返回主页
			toHome() {
				this.$mRouter.reLaunch({
					route: '/pages/index/index'
				});
			},
			// 获取手机验证码
			async getSmsCode() {
				this.reqBody['mobile'] = this.loginParams['mobile'];

				if (!checkSendCode) {
					this.$mHelper.toast(this.$mGraceChecker.error);
					return;
				}
				this.$mHelper.toast(`验证码发送成功, 请在手机上查看`);
				this.smsCodeBtnDisabled = true;
				uni.setStorageSync('pwdSmsCodeTime', moment().valueOf() / 1000);
				this.handleSmsCodeTime(59);
			},
			handleSmsCodeTime(time) {
				let timer = setInterval(() => {
					if (time === 0) {
						clearInterval(timer);
						this.smsCodeBtnDisabled = false;
					} else {
						this.codeSeconds = time;
						this.smsCodeBtnDisabled = true;
						time--;
					}
				}, 1000);
			},
			wxLogin() {
				let appid = 'wxc366fe748b0c40cd'
				// http://xdesigntest.natapp1.cc/saifu1/sys
				let redirect_uri = 'http%3A%2F%2Fxdesigntest.natapp1.cc/saifu1/sys' //'http://xd.designist.cn/' 
				let url =
					`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE&connect_redirect=1#wechat_redirect`;

				window.location.href = url;
				// uni.login({
				//   provider: 'weixin', //使用微信登录
				//   success: function (loginRes) {
				//     console.log(loginRes);
				//   },
				//   fail:function(err){
				// 	  console.log(err)
				//   }
				// });
			},
			// 提交表单
			async toLogin() {
				this.reqBody['username'] = this.loginParams['mobile'];
				this.reqBody['password'] = md5.hex_md5(this.loginParams['password']);
				this.reqBody.group = this.$mHelper.platformGroupFilter();
				this.btnLoading = true;
				console.log(this.reqBody, 'ceshi')
				userlogin(this.reqBody).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: '登录成功',
							duration: 2000
						});
						this.login(res.item);
						// uni.setStorageSync('accessToken',res.item.token)
						// uni.setStorageSync('user',res.item.user)
						// this.$mRouter.push('/pages/profile/profile');
						this.$mRouter.reLaunch({
							route: '/pages/profile/profile'
						});
					}
					this.btnLoading = false;
				}).catch(() => {
					uni.showToast({
						title: '网络错误，请稍后再试',
						duration: 2000
					});
					this.btnLoading = false;
				})
			},


		}
	};
</script>
<style lang="scss" scoped>
	page {
		background: #fff;
	}

	.logo-image {
		padding: 20rpx;
		
		uni-image {
			width: 110rpx;
			height: 140rpx;
		}

		image {
			width: 110rpx;
			height: 140rpx;
		}
	}
	.backgroud-image {
		uni-image {
			width: 80%;
			margin: auto;
		}
		image {
			width: 80%;
			margin: auto;
		}
	}

	.container {
		padding-top: 41px;
		position: relative;
		width: 100vw;
		overflow: hidden;
		background: #fff;

		.wrapper {
			position: relative;
			z-index: 90;
			background: #fff;
			padding-bottom: 40upx;
		}

		.back-btn {
			position: absolute;
			left: 40upx;
			z-index: 9999;
			padding-top: var(--status-bar-height);
			top: 40upx;
			font-size: 40upx;
			color: #d28b79;
		}

		.left-top-sign {
			font-size: 120upx;
			color: #6cbbff;
			position: relative;
			left: -16upx;
		}

		.right-top-sign {
			position: absolute;
			top: 80upx;
			right: -30upx;
			z-index: 95;

			&:before,
			&:after {
				display: block;
				content: '';
				width: 400upx;
				height: 80upx;
				background: #6cbbff;
			}

			&:before {
				transform: rotate(50deg);
				border-radius: 0 50px 0 0;
			}

			&:after {
				position: absolute;
				right: -198upx;
				top: 0;
				transform: rotate(-50deg);
				border-radius: 50px 0 0 0;
				/* background: pink; */
			}
		}

		.left-bottom-sign {
			position: absolute;
			left: -270upx;
			bottom: -320upx;
			border: 100upx solid #d0d1fd;
			border-radius: 50%;
			padding: 180upx;
		}

		.welcome {
			position: relative;
			left: 50upx;
			top: -90upx;
			font-size: 46upx;
			color: #001d6d;
			text-shadow: 1px 0px 1px rgba(0, 0, 0, 0.3);
		}

		.input-content {
			padding: 0 60upx;
		}

		.input-item {
			display: flex;
			flex-direction: column;
			align-items: flex-start;
			justify-content: center;
			padding: 0 30upx;
			background: #c7c7c730;
			height: 120upx;
			border-radius: 4px;
			margin-bottom: 50upx;

			&:last-child {
				margin-bottom: 0;
			}

			.tit {
				height: 50upx;
				line-height: 56upx;
				font-size: 24rpx;
				color: #000;
			}

			input {
				height: 60upx;
				font: size 24rpx;
				;
				color: #727272;
				width: 100%;
			}
		}

		.input-item-sms-code {
			position: relative;
			width: 100%;

			.sms-code-btn {
				position: absolute;
				color: #111;
				right: 20upx;
				bottom: 20upx;
				font-size: 28upx;
			}

			.sms-code-resend {
				color: #999;
			}

			.sms-code-btn:after {
				border: none;
				background-color: transparent;
			}
		}

		.forget-section {
			font-size: 24rpx;
			color: #000;
			text-align: center;
			margin-top: 40upx;
		}

		.register-section {
			margin: 30upx 0 50upx 0;
			width: 100%;
			font-size: 24rpx;
			color: #000;
			text-align: center;

			text {
				color: #000;
				margin-left: 10upx;
			}

			text:first-child {
				margin-right: 10upx;
			}
		}

		.btn-group {
			display: flex;
			margin-bottom: 20upx;
		}
	}
	.confirm-btn {
		height: 80upx;
		line-height: 80upx;
		background-color: #6cbbff;
		color: #fff;
	}
	.confirm-btn1 {
		width: 80%;
		height: 80upx;
		line-height: 80upx;
		background-color: #6cbbff;
		color: #fff;
	}
</style>